//
//  DiseaseDrugViewController.m
//  TestARC
//
//  Created by wuxiaoquan on 12-10-11.
//
//

#import "DiseaseDrugViewController.h"
#import "DBHelper.h"
#import "DrugResultViewController.h"
#import "DrugDetailViewController.h"
#define RowHight 44

@interface DiseaseDrugViewController ()

@end

@implementation DiseaseDrugViewController
@synthesize drugArray,drugTableView,hipLabel,changeControl,commonArray,brandArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([self.drugArray count] > 0) {
        self.hipLabel.text = @"参考治疗药物";
    }
    else
    {
        self.hipLabel.text = @"暂无参考治疗药物";
    }
    self.commonArray = [NSMutableArray arrayWithCapacity:5];
    self.brandArray = [NSMutableArray arrayWithCapacity:5];
    for (int i = 0; i<self.drugArray.count; i++) {
        if ([@"1" isEqualToString:[[self.drugArray objectAtIndex:i] objectForKey:@"flag"]]) {
            [self.brandArray addObject:[self.drugArray objectAtIndex:i]];
        }
        else
        {
            [self.commonArray addObject:[self.drugArray objectAtIndex:i]];
        }
    }
    [self addRoundBound:self.drugTableView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.drugArray =nil;
    self.drugTableView = nil;
    self.hipLabel = nil;
    self.changeControl = nil;
    self.commonArray = nil;
    self.brandArray = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//----------------tableview-----------------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int numberOfRows = 0;
    if (self.changeControl.selectedSegmentIndex == 0)
    {
        numberOfRows = self.commonArray.count;
    }
    else
    {
            numberOfRows = self.brandArray.count;
    }
//    CGRect oldFrame = tableView.frame;
//    CGPoint oldOrigin = oldFrame.origin;
//    CGFloat oldwidth =  oldFrame.size.width;
//    CGFloat newHight = numberOfRows*RowHight;
//    CGRect newFrame = CGRectMake(oldOrigin.x, oldOrigin.y, oldwidth, newHight);
//    
//    tableView.frame = newFrame;
    return  numberOfRows;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = nil;
    NSInteger row;
    
    cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
                                     reuseIdentifier:SimpleTableIdentifier];
    }
    row = [indexPath row];
    //NSLog(@"commArray at %d:%@",row,[commonArray objectAtIndex:row]);
    if (self.changeControl.selectedSegmentIndex == 0) {
        cell.textLabel.text = [[self.commonArray objectAtIndex:row]objectForKey:@"name"];
        cell.detailTextLabel.text = [[self.commonArray objectAtIndex:row]objectForKey:@"commonname"];
    }
    else
    {
        cell.textLabel.text = [[self.brandArray objectAtIndex:row]objectForKey:@"name"];
        cell.detailTextLabel.text = [[self.brandArray objectAtIndex:row]objectForKey:@"commonname"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //填装详细视图的内容。
    
    DrugDetailViewController * detailController = [[DrugDetailViewController alloc]initWithNibName:@"DrugDetailViewController" bundle:nil];
    
    int row = [indexPath row];
    //NSLog(@"选择了 %d 行",row);
    NSDictionary *drug;
    if (self.changeControl.selectedSegmentIndex == 0) {
        drug = [self.commonArray objectAtIndex:row];
    }
    else
    {
        drug = [self.brandArray objectAtIndex:row];
    }
    
    detailController.name =       [drug objectForKey:@"name"];
    detailController.brandName =  [drug objectForKey:@"brandname"];
    detailController.commonName = [drug objectForKey:@"commonname"];
    detailController.queryId =    [drug objectForKey:@"queryid"];
    
    [self.navigationController pushViewController:detailController animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    
}

-(IBAction)changeDrugView:(id)sender
{
    int index = self.changeControl.selectedSegmentIndex;
    if (index == 0) {
        NSLog(@"显示通用名");
        [self.drugTableView reloadData];
    }
    else
    {
        NSLog(@"显示商品名");
        [self.drugTableView reloadData];
    }
}

@end
